{include file="public/head"}
<style>
    .x-body {
        min-width: 900px;
    }

    body {
        overflow-x: scroll;
    }
</style>
<body>
<div class="x-body">
    <div class="lotus-nav">
        <button class="layui-btn layui-btn-sm"
                onclick="x_admin_show('新建','add.html')">
            <i class="layui-icon layui-icon-add-circle"></i>新建
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" onclick="delAll('{$Think.get.id}')">
            <i class="layui-icon"></i>批量删除
        </button>
        <span class="x-right">共有数据:<i class="i_count">{if count($list)>0}{$list->total()}{else/}0{/if}</i>条
        <button onclick="javascript:location.reload()" class="layui-btn-default ">
            <i class="layui-icon layui-icon-refresh"></i>
        </button>
        </span>
    </div>


    <table class="layui-table">
        <thead>
        <tr>
            <th style="width: 50px;text-align: center">
                <div class="header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
            </th>
            <th style="text-align:center">标题</th>
            <th style="width: 30px;text-align:center">排序</th>
            <td style="width: 30px;text-align:center">状态</td>
            <th style="width: 80px;text-align:center">创建用户</th>
            <th style="width: 120px;text-align:center">创建时间</th>
            <th style="width: 135px;text-align:center">操作</th>
        </thead>
        <tbody>
        {if count($list)>0}
        {volist name="list" id="vo"}
        <tr>
            <td style="text-align:center">
                <div class="layui-form-checkbox" lay-skin="primary" data-id='{$vo.id}'>
                    <i class="layui-icon">&#xe605;</i>
                </div>{$i}
            </td>
            <td>{$vo.title}</td>
            <td style="text-align:center">{$vo.odx}</td>
            <td>
                {switch name="vo.type" }
                {case value="1"}<b style="color: green">启用</b>{/case}
                {case value="2"}<b style="color: red">禁用</b>{/case}
                {/switch}
            </td>
            <td style="text-align:center">{$vo.truename}</td>
            <td style="text-align:center">{$vo['timestamp']|date="Y-m-d H:i",###}</td>
            <td style="text-align:center">
                {if $vo.type == 1}
                <button onclick="updateType(2,'{$vo.id}')" class="layui-btn layui-btn-xs layui-btn-primary">禁用</button>
                {else/}
                <button onclick="updateType(1,'{$vo.id}')" class="layui-btn layui-btn-xs layui-btn-normal">启用</button>
                {/if}
                <button class="layui-btn layui-btn-xs"
                        onclick="x_admin_show('编辑','edit.html?id={$vo.id}')">编辑
                </button>
                <button onclick="del(this,'{$vo.id}')" class="layui-btn layui-btn-xs layui-btn-danger">
                    删除
                </button>
            </td>
        </tr>
        {/volist}
        {else/}
        <tr>
            <td style="text-align: center;color:red" colspan="9">未找到符合条件的结果</td>
        </tr>
        {/if}
        </tbody>
    </table>
    {if count($list)>0}
    <div class="page">
        <div>
            {$list->render()}
        </div>
    </div>
    {/if}
</div>
</body>
{include file="public/foot"}
<script>
    function updateType(type, id) {
        if (type == 1) {
            var con = '启用';
        } else {
            var con = '禁用';
        }
        layer.confirm('确认要' + con + '此公告？', function (index) {
            layer.load(1);
            $.post("{:url('admin/notice/updateType')}", {
                type: type,
                id: id
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    //删除所有
    function delAll() {
        var data = tableCheck.getData();
        if (data.length == 0) {
            layer.msg("请选择需要删除的公告");
            return false;
        }
        layer.confirm('确认要全部删除吗？', function (index) {
            layer.load(1);
            $.post("{:url('admin/notice/delect')}", {
                id: data
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        location.reload();
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }

    function del(obj, id) {
        var count = $(".i_count")[0].innerText;
        layer.confirm('确认要删除吗？', function (index) {
            layer.load(1);
            $.post("{:url('admin/notice/delect')}", {
                id: id
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                        $(obj).parents('tr').remove();
                        $(".i_count").html(count - 1);
                    });
                } else {
                    layer.msg(data.msg, {icon: 2, time: 1000});
                }

            });
        });
    }
</script>
